package com.logbug.thread;

public class TestDeadLock {
    public static void main(String[] args) throws InterruptedException {
        Object lock1 = new Object();
        Object lock2 = new Object();
        new Thread(() -> {
            synchronized (lock1) {
                System.out.println("t1 running");
                try {
                    Thread.sleep(1000);
                    synchronized (lock2) {
                        System.out.println("t1 finished");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();
        new Thread(() -> {
            synchronized (lock2) {

                System.out.println("t2 running");
                try {
                    Thread.sleep(1000);
                    synchronized (lock1) {
                        System.out.println("t2 finished");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

        Object obj=new Object();
        synchronized (obj){
            //wait 方法必须在synchronized 里面调用
            obj.wait();
        }

    }
}
